const { QueryTypes } = require('sequelize');
const Goods = require('../models/goods');
const { sequelize } = require('../models/houses')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

exports.getGoods = async function () {
    const result = await sequelize.query("SELECT * FROM `goods`", { type: QueryTypes.SELECT });
    console.log(result);
    if (result) {
        return {
            data: result,
            meta: {
                msg: '获取数据成功',
                status: '200'
            }
        }
    } else {
        return {
            data: {},
            meta: {
                msg: '获取数据失败',
                status: '400'
            }
        }
    }
}

exports.getGoodstype = async function (type) {
    // console.log(id);
    const result = await sequelize.query("SELECT * FROM `goods` where type = :type",
        {
            replacements: {
                type
            },
            type: QueryTypes.SELECT
        },

    );
    console.log(result);
    if (result) { return { data: result, meta: { msg: '获取数据成功', status: '200' } } }
    return { data: {}, meta: { msg: '获取数据失败', status: '400' } }

}


exports.getgoodsimformation = async function (id) {
    console.log(id);
    const result = await sequelize.query("SELECT * FROM `goods` where id = :id",
        {
            replacements: {
                id
            },
            type: QueryTypes.SELECT
        },

    );
    console.log(result);
    if (result) { return { data: result, meta: { msg: '获取数据成功', status: '200' } } }
    return { data: {}, meta: { msg: '获取数据失败', status: '400' } }
}


exports.addGoods = async function (name, type, price) {

    let result = await Goods.create({
        name,
        type,
        price
    });
    result = result.toJSON()
    if (result) {
        return {
            data: { result },
            meta: {
                msg: '添加商品成功',
                status: '200'
            }
        }
    } else {
        return {
            data: {},
            meta: {
                msg: '添加商品失败',
                status: '400'
            }
        }
    }
}